﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBORpt = Texfina.BORpt.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;

namespace RH
{
   public partial class wfCTSTrabRpt : EditPage
   {
      private Font printFont;
      private int _indexRow = 0;
      private int _NumberPage = 0;

      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();

      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = FormEditAction.Edit;
         this.SetInitialTitle("Constancia de deposito CTS");
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
         ECCosto objECCosto = new ECCosto();

         IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = ConstMultitabla.MES;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         //WebHelper.LoadListControl(ddlidMes, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);

         objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
         dt = objBOI.GetDetailByList(objEDet);
         //WebHelper.LoadListControl(ddlidFormaPago, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);

         //objECCosto.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         //dt = objBOCCosto.GetList(objECCosto);
         //WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", ConstCustomFirstListItem.ITEM_ALL);

         SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
         EEmpEstable objEEmp = new EEmpEstable();
         objEEmp.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         dt = objBOEmp.GetEstablecimientos(objEEmp);
         WebHelper.LoadListControl(ddlidEstable, dt, "dsEstable", "idEstable", ConstCustomFirstListItem.ITEM_ALL);

         IBOQuery objBOUNego = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.UNegocio));
         EUNegocio objEUNeg = new EUNegocio();
         objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         dt = objBOUNego.GetList(objEUNeg);
         WebHelper.LoadListControl(ddlidUniNego, dt, "dsUNegocio", "idUNegocio", ConstCustomFirstListItem.ITEM_ALL);


      }

      protected void ddlidUNegocio_SelectedIndexChanged(object sender, EventArgs e)
      {
         ddlidCCosto.Items.Clear();
         EUNegocio objEUNeg = new EUNegocio();
         MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
         objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEUNeg.IdUNegocio = ddlidUniNego.SelectedItem.Value;
         DataTable dt = objBOUNego.GetCCostoXUNegocio(objEUNeg);
         WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", true, true);
      }

      public override void RegisterGeneralScripts()
      {

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('wfPersonal.aspx',725,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1];");
         this.txtidPersonal.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         //sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMes.ClientID + "','Seleccione el Mes.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRet = modalDialog('wfProcPlani.aspx?IdMes=' + document.getElementById('" + this.ddlidMes.ClientID + "').value + '&IdFormaPago=' + document.getElementById('" + this.ddlidFormaPago.ClientID + "').value,725,400);");
         //sb.AppendLine("if(varRet==null)");
         //sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnnuProcAnual.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");

         sb = new StringBuilder();

         //sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMes.ClientID + "','Seleccione el Mes.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("var varRet = modalDialog('wfProcPlani.aspx?IdMes=' + document.getElementById('" + this.ddlidMes.ClientID + "').value + '&IdFormaPago=' + document.getElementById('" + this.ddlidFormaPago.ClientID + "').value,725,400);");
         //sb.AppendLine("if(varRet==null)");
         //sb.AppendLine("return false;");
         sb.AppendLine("if(varRet[0] != document.getElementById('" + this.hdnidPlanilla.ClientID + "').value){");
         sb.AppendLine("alert('No puede seleccionar un tipo de planilla diferente.'); return false;}");
         sb.AppendLine("else if(varRet[4] < document.getElementById('" + this.hdnnuProcAnual.ClientID + "').value){");
         sb.AppendLine("alert('No puede seleccionar una Planilla con numero de proceso menor.'); return false;}");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnnuProcAnualFin.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");

         sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");
         //sb.AppendLine("      var varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione la Planilla');");
         sb.AppendLine("      if(varRpta==true)");
         sb.AppendLine("      return !varRpta;");
         sb.AppendLine("__doPostBack('" + this.mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");


         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {

            case ConstMenuToolbarValue.BUTTON_PREVIEW:
               PreviewReport(e.Item.Value);
               break;
            case ConstMenuToolbarValue.BUTTON_EXPORT:
               ExportReport(e.Item.Value);
               break;
         }

      }

      public void PreviewReport(string key)
      {
         ReportDocument objRpt = new ReportDocument();

         DataSet ds = (DataSet)this.GetDataReport(key);

         DataTable dt = ds.Tables[0];

         string NomRep = "";

         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }


         NomRep = "Certificado5ta";
         objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/ConstanciaCTS.rpt"));



         objRpt.SetDataSource(ds.Tables[0]);

         MemoryStream ms = new MemoryStream();

         Response.Clear();

         ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
         objRpt.Dispose();
         objRpt = null;

         Response.AddHeader("Content-Disposition", "attachment;filename=" + NomRep + ".pdf");
         Response.Buffer = true;
         Response.ContentType = "application/pdf";


         
         ms.WriteTo(Response.OutputStream);
         Response.Flush();
         Response.End();

      }

      public override void ExportReport(string key)
      {
         DataSet ds = (DataSet)this.GetDataReport(key);
         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }
         ExcelHelper.ToExcel(ds, "Data.xls", Response);

      }

      public override object GetDataReport(string key)
      {

         EConstCTS objEDet = new EConstCTS();
         RHBORpt.IPlanilla objBODet = (RHBORpt.IPlanilla)WCFHelper.GetObject<RHBORpt.IPlanilla>(typeof(RHBORpt.Planilla));
         objEDet.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         //objEDet.IdForPago = this.ddlidFormaPago.SelectedItem.Value;
         //objEDet.IdPlanilla = this.hdnidPlanilla.Value;


         if (this.txtidPersonal.Text == "")
            objEDet.IdPersonal = null;
         else
            objEDet.IdPersonal = this.hdnidPersonal.Value;

         if (this.ddlidCTS.Text == "")
            objEDet.IdCTS = null;
         else
            objEDet.IdCTS = this.ddlidCTS.SelectedItem.Value;

         if (this.ddlidPlanilla.Text == "")
            objEDet.IdPlanilla = null;
         else
            objEDet.IdPlanilla = this.ddlidPlanilla.SelectedItem.Value;

         if (this.ddlidEstable.SelectedItem.Value == "")
            objEDet.IdEstable = null;
         else
            objEDet.IdEstable = this.ddlidEstable.SelectedItem.Value;

         if (this.ddlidUniNego.SelectedItem.Value == "")
            objEDet.IdUNegocio = null;
         else
            objEDet.IdUNegocio = this.ddlidUniNego.SelectedItem.Value;

         if (this.ddlidCCosto.SelectedItem != null)
            if (this.ddlidCCosto.SelectedItem.Value == "")
               objEDet.IdCCosto = null;
            else
               objEDet.IdCCosto = this.ddlidCCosto.SelectedItem.Value;
         else
            objEDet.IdCCosto = null;

         DataSet ds = new DataSet();

         DataTable dt = null;

         dt = objBODet.GetConstanciaCTS(objEDet);
         ds.Tables.Add(dt);

         return ds;


      }

   }
}